<?php
//VIP 折扣插件
//By avenger
//$Id: vip.php 9 2009-04-26 06:35:12Z bole.Chen $
//SQL: ALTER TABLE `users` ADD `vip` INT NOT NULL;
//SELECT uid,linkman,cman,sum(sumfee+adjustfee) AS fee FROM `indent` WHERE stat >=3 GROUP BY uid ORDER BY fee DESC

@session_start();

//满 xxx 元成为VIP
define('VIP_MINFEE', 500);

//VIP 折扣
define('VIP_OFF', 0.05);


//登入处理
add_action('users_login_succeed', 'vip_users_login_succeed');

if (!$_SESSION['vip']) return;

if ($_SESSION['welcome_msg'] && (strpos($_SESSION['welcome_msg'], 'vip.gif') === false)) $_SESSION['welcome_msg'] = '&nbsp;<a href="/blog/?p=515" target="_blank" class="nohv"><img title="VIP" src="/images/vip.gif" border="0" width="17" height="8" alt="VIP" /></a>'.$_SESSION['welcome_msg'];

//购物车页显示 vip 折扣
add_action('view_shopcar_list_itemlist_after', 'vip_view_shopcar_list_itemlist_after');
add_action('view_shopcar_checkout_itemlist_after', 'vip_view_shopcar_checkout_itemlist_after');

function vip_view_shopcar_list_itemlist_after($list) {
    global $_vipoff;

    if ($_vipoff) echo '<tr style="background:#CCC;color:red"><td style="padding:10px" class="px12">VIP 折扣 [<a href="/blog/?p=515" target="_blank">?</a>]</td><td>-</td><td class="px12">- &yen; '. $_vipoff .'</td><td>&nbsp;</td></tr>';
}

function vip_view_shopcar_checkout_itemlist_after($list) {
    global $_vipoff;

    if ($_vipoff) echo '<tr style="background:#CCC;color:red"><td style="padding:10px" class="px12">VIP 折扣</td><td>&nbsp;</td><td class="px12">- &yen; '. $_vipoff .'</td></tr>';
}

//订单折扣处理
add_action('shopcar_shopCarList', 'vip_shopcar_shopCarList');
add_action('shopcar_checkout_befor', 'vip_shopcar_checkout_befor');

function vip_shopcar_shopCarList($result) {
    global $_vipoff;

    foreach ($result['list'] as $v) $sumfee += $v['price'] * $v['amount'];
    $_vipoff = round($sumfee*VIP_OFF, 1);
    $result['totalprice'] -= $_vipoff;
}

function vip_shopcar_checkout_befor($data) {
    global $_vipoff;

    if ($_vipoff) $data['adjustfee'] -= $_vipoff;
}

//登入登出处理
add_action('users_logout', 'vip_users_logout');

function vip_users_logout() {
    unset($_SESSION['vip']);
    unset($_SESSION['welcome_msg']);
}

function vip_users_login_succeed($result) {
    $_SESSION['vip'] = $result['vip'];
}

//后台修改
add_action('view_admin_usercustomfields', 'vip_view_admin_usercustomfields', 5);
add_action('view_admin_eachuser', 'vip_view_admin_eachuser');
add_action('users_adminedit_befor', 'vip_users_adminedit_befor');

//后台用户修改
function vip_view_admin_usercustomfields($data) {
    $checked = $data['vip'] ? ' checked' : '';
    echo <<< F
<tr><td nowrap>VIP?</td><td style="text-align:left"><input type="checkbox" name="vip" value="1"$checked /></td></tr>
F;
}

function vip_users_adminedit_befor($data) {
    if ($_SESSION['admin'] && $_POST['vip']) $data['fields']['vip'] = (int)$_POST['vip'];
}

function vip_view_admin_eachuser(&$user) {
    if ($user['vip']) $user['truename'] = '<img src="/images/vip.gif" />&nbsp;'.$user['truename'];
}

//自动 VIP 处理
add_action('indent_adminedit_befor', 'vip_indent_adminedit_befor');

function vip_indent_adminedit_befor($data) {
    $db =& getDB();

    $uid = $db->GetOne(getTable('indent'), 'uid', "id={$data['id']}");
    $userinfo = $db->GetRow(getTable('users'), '*', "id=$uid");

    if ($userinfo['vip'] == 0) {
        $totalfee = $db->GetOne(getTable('indent'), 'SUM(sumfee+adjustfee)', "uid=$uid AND stat>=3");
        if ($totalfee >= VIP_MINFEE) {
            $db->Update(getTable('users'), array('vip'=>1), "id=$uid");

            //发mail
            $f['subject']   =   "您成为乐铺的 VIP 会员了";
            $f['mailto']    =   $userinfo['email'];
            $f['body']      =   "亲爱的{$userinfo['nickname']}：\r\n\r\n";
            $f['body']      .=  "感谢您一直以来对乐铺的支持和信任。今天，我们高兴的通知您，您已经成为乐铺为数不多的 VIP 会员之一。\r\n\r\n从即日起，用您的用户名登录乐铺便可享有全场 95 折的 VIP 价格。快去看看吧，您在使用我们服务的过程中，有任何不满意的地方，请及时反馈给我们，乐铺期待为您提供更优质的服务！\r\n\r\n";
            $f['created']   =   time();
            $db->Insert(getTable('mail'), $f);
        }
    }
}
?>
